﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace V5_Common
{
    public class Filter
    {
        /// <summary>
        /// 过滤HTML
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearHTML(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = Content.Replace("<", "&lt;");
                Content = Content.Replace(">", "&gt;");
            }
            return Content;
        }

        /// <summary>
        /// 清除frame
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearIframe(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                string str = "<iframe|</iframe|<frame|</frame";
                foreach (string str2 in str.Split(new char[] { '|' }))
                {
                    Content = Regexs(Content, str2, "&lt;" + str2.Substring(1));
                }
            }
            return Content;
        }

        /// <summary>
        /// 清除javascript
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearJS(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = ClearIframe(Content);
                Content = Content.Replace("/*", @"\*");
                Content = Content.Replace("*/", @"*\");
                string str = "position|left|top";
                foreach (string str2 in str.Split(new char[] { '|' }))
                {
                    Content = Regexs(Content, @"( style[\s]*=[\s\S]*?)" + str2 + @"[\s]*:[\s]*\w+[\s;]*", "$1");
                }
                string str3 = "<script|</script|cookie|alert|expression|eval|escape|write";
                foreach (string str4 in str3.Split(new char[] { '|' }))
                {
                    Content = Regexs(Content, str4, "<span>" + str4.Substring(0, 1) + "</span>" + str4.Substring(1));
                }
            }
            return Content;
        }
        /// <summary>
        /// 过滤HTML
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string KillHTML(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = Regex.Replace(Content, "<[^>]+>", "");
                Content = ClearJS(Content);
            }
            return Content;
        }
        /// <summary>
        /// 过滤日文
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string KillJapan(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = ClearIframe(Content);
                Content = Content.Replace("ガ", "&#12460;");
                Content = Content.Replace("ギ", "&#12462;");
                Content = Content.Replace("ア", "&#12450;");
                Content = Content.Replace("ゲ", "&#12466;");
                Content = Content.Replace("ゴ", "&#12468;");
                Content = Content.Replace("ザ", "&#12470;");
                Content = Content.Replace("ジ", "&#12472;");
                Content = Content.Replace("ズ", "&#12474;");
                Content = Content.Replace("ゼ", "&#12476;");
                Content = Content.Replace("ゾ", "&#12478;");
                Content = Content.Replace("ダ", "&#12480;");
                Content = Content.Replace("ヂ", "&#12482;");
                Content = Content.Replace("ヅ", "&#12485;");
                Content = Content.Replace("デ", "&#12487;");
                Content = Content.Replace("ド", "&#12489;");
                Content = Content.Replace("バ", "&#12496;");
                Content = Content.Replace("パ", "&#12497;");
                Content = Content.Replace("ビ", "&#12499;");
                Content = Content.Replace("ピ", "&#12500;");
                Content = Content.Replace("ブ", "&#12502;");
                Content = Content.Replace("ブ", "&#12502;");
                Content = Content.Replace("プ", "&#12503;");
                Content = Content.Replace("ベ", "&#12505;");
                Content = Content.Replace("ペ", "&#12506;");
                Content = Content.Replace("ボ", "&#12508;");
                Content = Content.Replace("ポ", "&#12509;");
                Content = Content.Replace("ヴ", "&#12532;");
            }
            return Content;
        }

        public static string Regexs(string html, string str1, string str2)
        {
            Regex regex = new Regex(str1, RegexOptions.IgnoreCase);
            return regex.Replace(html, str2);
        }

        /// <summary>
        /// 过滤SQL
        /// </summary>
        /// <param name="sInput"></param>
        /// <returns></returns>
        public static string FilterSQL(string sInput)
        {
            if ((sInput == null) || (sInput == ""))
            {
                return null;
            }
            string sInput1 = sInput.ToLower();
            string output = sInput;
            string pattern = "*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
            if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
            {
                throw new Exception("字符串中含有非法字符!");
            }
            return output.Replace("'", "''");
        }

    }
}
